package cn.hist.hfp.controller;

import cn.hist.hfp.RoleType;
import cn.hist.hfp.entity.CommodityOrder;
import cn.hist.hfp.entity.PetOrder;
import cn.hist.hfp.service.OrderService;
import cn.hist.hfp.util.Authentication;
import cn.hist.hfp.util.MyResponse;
import cn.hist.hfp.vo.CommodityOrderVO;
import cn.hist.hfp.vo.PetOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    private final
    Authentication authentication;

    private final OrderService orderService;

    @Autowired
    public OrderController(Authentication authentication, OrderService orderService) {
        this.authentication = authentication;
        this.orderService = orderService;
    }

    @PostMapping("/pet")
    public ResponseEntity<Integer> createPetOrder(@RequestBody PetOrder petOrder, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.USER);
        PetOrder storedPetOrder = orderService.createPetOrder(petOrder);

        MyResponse<Integer> myResponse = new MyResponse<>();
        if (storedPetOrder != null)
            return myResponse.response(200, authentication.refreshToken(token), storedPetOrder.getId());
        return myResponse.response(200, authentication.refreshToken(token), -1);
    }

    @GetMapping("/pet")
    public ResponseEntity<List<PetOrderVO>> findAllPetOrders(@RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        MyResponse<List<PetOrderVO>> myResponse = new MyResponse<>();
        return myResponse.response(200, authentication.refreshToken(token), orderService.findAllPetOrders());
    }

    @GetMapping("/pet/{user_id}")
    public List<PetOrderVO> findAllPetOrdersByUser(@PathVariable int user_id) {
        return orderService.findAllPetOrdersByUser(user_id);
    }

    @PostMapping("/commodity")
    public ResponseEntity<Integer> createCommodityOrder(@RequestBody CommodityOrder commodityOrder, @RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.USER);
        CommodityOrder storedCommodityOrder = orderService.createCommodityOrders(commodityOrder);

        MyResponse<Integer> myResponse = new MyResponse<>();
        if (storedCommodityOrder != null)
            return myResponse.response(200, authentication.refreshToken(token), storedCommodityOrder.getId());
        return myResponse.response(200, authentication.refreshToken(token), -1);
    }

    @GetMapping("/commodity")
    public ResponseEntity<List<CommodityOrderVO>> findAllCommodityOrders(@RequestHeader String token) {
        authentication.permissionCheck(token, RoleType.MANAGER);
        MyResponse<List<CommodityOrderVO>> myResponse = new MyResponse<>();
        return myResponse.response(200, authentication.refreshToken(token), orderService.findAllCommodityOrders());
    }

    @GetMapping("/commodity/{user_id}")
    public List<CommodityOrderVO> findAllCommodityOrdersByUser(@PathVariable int user_id) {
        return orderService.findAllCommodityOrdersByUser(user_id);
    }
}
